package com.tech.view.common;

import com.tech.view.component.border.RoundBorder;

import javax.swing.*;
import java.awt.*;

/**
 * 按钮工厂类
 */
public class ButtonFactory {
	public static JButton createDefaultButton(String text) {
		return createDefaultButton(text, null, 0, 0);
	}

	public static JButton createDefaultButton(String text, Color backgroundColor) {
		return createDefaultButton(text, backgroundColor, 0, 0);
	}

	public static JButton createDefaultButton(String text, Color backgroundColor, int width, int height) {
		if (backgroundColor == null)
			backgroundColor = ColorUtil.LIGHT_BLUE_COLOR;
		JButton button = new JButton(text);
		button.setBackground(backgroundColor);
		button.setForeground(ColorUtil.TITLE_FONT_COLOR);
		button.setBorderPainted(false);
		if (width > 0 && height > 0) {
			button.setPreferredSize(new Dimension(width, height));
			button.setFont(FontUtil.DEFAULT_FONT.deriveFont(height * 0.6f));
		}
		return button;
	}

	public static JButton createPlainButton(String text) {
		return createPlainButton(text, null, 0, 0);
	}

	public static JButton createPlainButton(String text, Color borderColor, int width, int height) {
		JButton button = new JButton(text);
		button.setBackground(ColorUtil.BACKGROUND_COLOR);
		if (width > 0 && height > 0) {
			button.setPreferredSize(new Dimension(width, height));
			button.setFont(FontUtil.DEFAULT_FONT.deriveFont(height * 0.6f));
		}
		if (borderColor != null)
			button.setBorder(BorderFactory.createLineBorder(borderColor, 1, true));
		return button;
	}

	public static JButton createIconButton(String text, Image image) {
		return createIconButton(0, 0, text, image, SwingConstants.CENTER);
	}

	/**
	 * 生成包含图标和文字的按钮，图标和文字水平布局
	 *
	 * @param width     宽度，使用默认就填0
	 * @param height    高度，使用默认就填0
	 * @param text      文本
	 * @param image     左侧图标，没有填null
	 * @param alignment 对齐方式，用于控制指定宽高下文本和图标的位置，建议使用SwingConstants的常量
	 */
	public static JButton createIconButton(int width, int height, String text, Image image, int alignment) {
		JButton button = image == null ? new JButton(text) : new JButton(text, new ImageIcon(image));
		if (width > 0 && height > 0)
			button.setPreferredSize(new Dimension(width, height));
		button.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

		button.setHorizontalAlignment(alignment);
		button.setBackground(ColorUtil.BACKGROUND_COLOR);

		button.setFont(FontUtil.DEFAULT_FONT.deriveFont(12f));
		return button;
	}

	/**
	 * 创建没有文本的按钮，用于轮播图下方的按钮
	 */
	public static JButton createNoTextButton(int width, int height, Color backgroundColor) {
		JButton button = new JButton();
		button.setPreferredSize(new Dimension(width, height));
		button.setBorder(null);
		button.setBackground(backgroundColor);
		return button;
	}

	public static void main(String[] args) {
		SwingTestUtils.loadSkin();
		SwingTestUtils.test(createIconButton(200, 50, "登录", ResourcesUtil.getImage(ResourcesUtil.Image.LOGO), SwingConstants.LEFT));
		// SwingTestUtils.test(createNoTextButton(100, 30, ColorUtil.SHALLOW_FONT_COLOR));
	}
}
